package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.net.CacheRequest;
import java.util.HashMap;
import java.util.Map;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;



    @Autowired
    private MongoTemplate mongoTemplate;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 查询用户行为
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //创建返回对象
        Map<String, Boolean> map = new HashMap<>();
        //1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null) {
            //2. 未登录 直接将 4种行为 均返回false
            map.put("isfollow", false);
            map.put("islike", false);
            map.put("isunlike", false);
            map.put("iscollection", false);
            return ResponseResult.okResult(map);
        }

        //3. 如果登录
        //4. 查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), null);
        if (apBehaviorEntry == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId())
                .and("articleId").is(dto.getArticleId()));
        //5. 根据行为实体 文章ID查询 是否点赞
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if (apLikesBehavior == null || apLikesBehavior.getOperation() == 1){
            map.put("islike",false);
        }else {
            map.put("islike",true);
        }

        //6. 根据行为实体 文章ID查询 是否不喜欢
        ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if (apUnlikesBehavior == null || apUnlikesBehavior.getType() == 1){
            map.put("isunlike",false);
        }else {
            map.put("isunlike",true);
        }
        //7. 根据行为实体 文章ID查询 是否收藏
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        if (apCollection == null || apCollection.getOperation() == 1){
            map.put("iscollection",false);
        }else {
            map.put("iscollection",true);
        }
        //8. 更具登录用户id 去redis中查询是否关注该作者
        Double score = stringRedisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), dto.getAuthorApUserId().toString());
        if (score == null){
            map.put("isfollow",false);
        }else {
            map.put("isfollow",true);
        }

        //9. 封装结果 返回
        return ResponseResult.okResult(map);
    }
}
